package com.xq.shop.vo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.xq.shop.vo.SearchValue.Order;
import com.xq.shop.vo.SearchValue.Pattern;

public class SearchObject {

	private List<SearchValue> condition = new ArrayList<SearchValue>();
	private int pageNum;
	private int pageSize;
	
	public SearchObject() {
	}
	
	public SearchObject(int pageNum, int pageSize) {
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}
	
	public void addCondition(String name, Object value, Pattern pattern){
		condition.add(new SearchValue(name, value, pattern));
	}
	
	/**
	 * 获取排序参数
	 * @return
	 */
	public Map<String, Order> getOrder(){
		
		Map<String, Order> map = new LinkedHashMap<String, Order>();
		
		List<SearchValue> list = new ArrayList<SearchValue>(this.condition);
		
		Collections.sort(list, new Comparator<SearchValue>() {

			@Override
			public int compare(SearchValue o1, SearchValue o2) {
				if(o1.getOrderSeq() > o2.getOrderSeq()){
					return 1;
				} else {
					return 0;
				}
			}
		});
		
		for(SearchValue search : list){
			
			if(StringUtils.isBlank(search.getName()) || search.getOrder() == null){
				continue;
			}
			map.put(search.getName(), search.getOrder());
		}
		
		return map;
	}
	
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public List<SearchValue> getCondition() {
		return condition;
	}
	public void setCondition(List<SearchValue> condition) {
		this.condition = condition;
	}
}